package com.lei.sort;

import com.lei.linearsearch.ArrayGenerator;

/**
 * @Author LeiXiaoLei
 * @Date 2022/7/14 18:50
 * @Version 1.0
 */
public class SelectionSort {
    private SelectionSort() {
    }

    public static <E extends Comparable<E>> void sort(E[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j].compareTo(arr[minIndex]) < 0) {
                    minIndex = j;
                }
            }
            swap(arr, i, minIndex);
        }
    }

    public static <E extends Comparable<E>> void sort2(E[] arr) {
        for (int i = arr.length - 1; i >= 0; i--) {
            int maxIndex = i;
            for (int j = i - 1; j >= 0; j--) {
                if (arr[j].compareTo(arr[maxIndex]) > 0) maxIndex = j;
            }
            swap(arr, i, maxIndex);
        }
    }

    private static <E> void swap(E[] arr, int i, int j) {
        E t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public static void main(String[] args) {
//        Integer[] arr = {1, 4, 2, 3, 6, 5};
//        SelectSort.sort(arr);
//        for (int i : arr) {
//            System.out.print(i + " ");
//        }
//        System.out.println();
//        Student[] students = {
//                new Student("张三", 99),
//                new Student("李四", 45),
//                new Student("王五", 21),
//                new Student("赵六", 78),
//                new Student("王老七", 80),
//                new Student("王老八", 99)
//        };
//        SelectSort.sort(students);
//        for (Student student : students) {
//            System.out.println(student + " ");
//        }
        int[] dataSize = {10, 100, 1000, 10000, 100000};
        for (int n : dataSize) {
            Integer[] arr = ArrayGenerator.generatorRandomArray(n, n);
            SortingHelper.sortTest(SelectionSort.class, arr);
            SelectionSort.sort2(arr);
        }

    }
}
